package cc.langk.sms.service;

import cc.langk.sms.dao.TencentSmsHistoryRepository;
import cc.langk.sms.entity.AliSmsHistory;
import cc.langk.sms.entity.TencentSmsHistory;
import cc.langk.sms.handler.AliHandler;
import cc.langk.sms.handler.TencentHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsHistortService {


    @Autowired
    private AliHandler aliHandler;

    @Autowired
    private TencentHandler tencentHandler;

    @Autowired
    private TencentSmsHistoryRepository tencentSmsHistoryRepository;

    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    public boolean sendAliSms(String phone) {
        AliSmsHistory smsHistory = new AliSmsHistory();
        smsHistory.setPhoneNumbers(phone);
        smsHistory = aliHandler.sendSms(smsHistory);
        if (smsHistory.getCode() != null && smsHistory.getCode().equals("OK")) {
            //请求成功
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    public boolean sendTencentSms(String phone) {
        TencentSmsHistory smsHistory = new TencentSmsHistory();
        smsHistory.setMobile(phone);
        smsHistory = tencentHandler.sendSingleSms(smsHistory);
        smsHistory.setCreateTime(new Date());
        tencentSmsHistoryRepository.save(smsHistory);
        if (smsHistory.getResult() != null && smsHistory.getResult().checkSuccess()) {
            //请求成功
            return true;
        } else {
            return false;
        }
    }
}
